package com.neusoft.neuiotms.dcom.service.alter.impl;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.dcom.converter.alter.AlterCountersignatureApprovalConverter;
import com.neusoft.neuiotms.dcom.dto.alter.AlterCountersignatureApprovalDTO;
import com.neusoft.neuiotms.dcom.model.alter.AlterCountersignatureApproval;
import com.neusoft.neuiotms.dcom.repository.alter.AlterCountersignatureApprovalRepository;
import com.neusoft.neuiotms.dcom.service.alter.AlterCountersignatureApprovalService;

@Service
public class AlterCountersignatureApprovalServiceImpl implements AlterCountersignatureApprovalService {

    @Autowired
    private AlterCountersignatureApprovalConverter countersignatureApprovalConverter;
    @Autowired
    private AlterCountersignatureApprovalRepository countersignatureApprovalRepository;
    @Autowired
    private ProcessInstanceService processInstanceService;

    @Override
    public AlterCountersignatureApprovalDTO create(final AlterCountersignatureApprovalDTO dto,
            final UserBean user) {

        // 设置当前节点操作人
        dto.setOperatorCode(user.getUsername());
        dto.setOperatorName(user.getName());

        // 设置流程变量是否同意
        this.processInstanceService.setGlobalVariable(dto.getTaskId(),
                DCOMConstants.ALTER_PROCESS_LEADER_OR_SUPERVISOR_AGREES, dto.getWhetherAgree());
        this.processInstanceService.setGlobalVariable(dto.getTaskId(),
                "passFlag", dto.getWhetherAgree());

        // 完成节点
        this.processInstanceService.complete(dto.getWorkNum(), dto.getTaskId(), user);

        // 查询当前workNum和TaskId下是否存在记录
        final AlterCountersignatureApproval oldModel =
                this.countersignatureApprovalRepository.findByWorkNumAndTaskId(
                        dto.getWorkNum(), dto.getTaskId());

        // 如果不存在新增
        if (oldModel == null) {
            dto.setId(null);
        }

        // 保存操作信息
        final AlterCountersignatureApproval model = this.countersignatureApprovalConverter.toModel(dto);
        this.countersignatureApprovalRepository.save(model);
        return this.countersignatureApprovalConverter.toDTO(model);
    }

    @Override
    public AlterCountersignatureApprovalDTO detail(final String workNum, final String taskId,
            final UserBean user) {

        AlterCountersignatureApproval model = null;
        try {
            if (StringUtils.isNotBlank(workNum) && !DCOMConstants.STR_NULL.equals(taskId)) {
                model = this.countersignatureApprovalRepository.findByWorkNumAndTaskId(workNum, taskId);
                if (model == null) {
                    // 获取前一节点的TaskID
                    final String fromProcessTaskId = this.processInstanceService.getLastTaskId(taskId, user);
                    if (fromProcessTaskId == null) {
                        return new AlterCountersignatureApprovalDTO();
                    } else {
                        model = this.countersignatureApprovalRepository.findByWorkNumAndTaskId(workNum,
                                fromProcessTaskId);
                    }
                }
            } else if (DCOMConstants.STR_NULL.equals(taskId)) {
                model = this.countersignatureApprovalRepository.findTopByWorkNumOrderByIdDesc(workNum);
            }
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                    new Object[] {workNum },
                    new Object[] {taskId });
        }
        return this.countersignatureApprovalConverter.toDTO(model);
    }

}
